<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Exception;
use Illuminate\Support\Facades\Session;

class ProductController extends Controller {


        public function list(){
            $role_id = Session::get('role_id');
            if ($role_id == 1){
                $product = DB::table('product')
                    ->select('*')
                    ->get();
            }else if($role_id == 2) {
                $product = DB::table('product')
                    ->select('*')
                    ->where('company_id', Session::get('session_id'))
                    ->get();
            }else if($role_id == 3) {
                $product = DB::table('product')
                    ->select('*')
                    ->where('company_id', Session::get('company_id'))
                    ->get();
            }
            return view('/admin/product/list',['product'=>$product]);
        }

        /*
         * 产品删除
         * */

        public function delete(Request $request){
             $bool = DB::table('product')->where('id',$request->product_id)->delete();
             echo "<script language='javascript'>alert('删除成功');window.location.href='/admin/product/list';</script>";
        }


        public function add(){

           $role_id = Session::get('role_id');
            if($role_id == 2){
                  $supplyer_company = DB::table('supplyer_company')
                       ->select('id','name','pay_address')
                       ->where('company_id',Session::get('session_id'))
                       ->get();
                  $supplyer_table = json_decode($supplyer_company,1);
                  if(empty($supplyer_table)){
                    $res = "<script language='javascript'>alert('进入失败，必须添加供应商');window.location.href='/admin/product/list';</script>";
                    return $res;
                  }
                    $supplyer = [];
                    $supplyer_id = [];
                   foreach ($supplyer_company as $key=>$value){
                        $supplyer[] = $value->name;
                        $supplyer_id[] = $value->id;
                   }
                    return view('/admin/product/add',['supplyer'=>$supplyer,'supplyer_id'=>$supplyer_id]);
            }else{
                    echo "<script language='javascript'>alert('进入失败，必须公司账号');window.location.href='/admin/product/list';</script>";
            }

        }


        public function add_do(Request $request){
            $product = $request->all();
            foreach ($product as $key=>$value){
                if(empty($value)){
                    header('Content-Type:text/html;charset=utf-8');
                    echo "<script language='javascript'>alert('添加失败,不能为空');window.location.href='/admin/product/add';</script>";
                    exit;
                }
            }
            unset($product['_token']);
            $supplyer_id = $product['supplyer_id'];
            unset($product['supplyer_id']);
            $supplyer = DB::table('supplyer_company')
                ->where('id',$supplyer_id)
                ->where('company_id',Session::get('session_id'))
                ->select('id','name','pay_address','company_name','company_id','pay_position')
                ->first();
            $product['pay_position'] = $supplyer->pay_position;
            $product['supplyer_company'] = $supplyer->name;
            $product['company_id'] = $supplyer->company_id;
            $product['pay_address'] = $supplyer->pay_address;
            $product['pay_name'] = $supplyer->company_name;
            $product['company_name'] = $supplyer->company_name;
            $product['product_name'] = $product['brand'].$product['category'];

            $id = DB::table('product')->insertGetId($product);
            if($id){
                echo '<script language="javascript">window.location.href="/admin/product/upload?product_id='.$id.'";</script>';
            }else{
                echo "<script language='javascript'>alert('添加失败');window.location.href='/admin/product/add';</script>";
            }
        }

        /*
         * 上传图片
         * */

        public function upload(Request $request){

            return view('/admin/product/upload',['product_id'=>$request->product_id]);
        }

        /*
         * 上传图片处理
         * */

        public function upload_do(Request $request){
            $file = $request->file;
            $product = DB::table('product')->select('img_id')->where('id',$request->product_id)->first();
            $img_id = $product->img_id;
            foreach ($file as $key=>$value){
                  $name = $this->image($value);
                  if(!empty($name)) {
                      $id = DB::table('product_images')->insertGetId(array('name' => $name));
                      $img_id .= $id.',';
                  }
            }

            DB::table('product')->where('id',$request->product_id)->update(array('img_id'=>$img_id));
        }


        /*
         * 查看产品图片
         * */

        public function img(Request $request){

            $product = DB::table('product')->select('img_id')->where('id',$request->product_id)->first();
            $img_id = explode(',',$product->img_id);
            $img = DB::table('product_images')
                ->select('id','name')
                ->whereIn('id',$img_id)->get();

            $list = [];
            foreach ($img as $key=>$value){
                $list[]['name'] = '/product/'.$value->name;
            }

            return view('/admin/product/img',['list'=>$list]);
        }



        public function image($file){
            if($file->getmimeType() == "image/png" || $file->getmimeType() == "image/jpeg"){
                $extension = $file->extension();//图片路径
                $name = date('YmdHis',time()).rand(00000000,99999999999).'.'.$extension;
                $file->move("./product",$name);//移动至框架图片文件夹
                return $name;
            }
       }


       /*
        * 价格
        * */

       public function price(Request $request){

            $today = strtotime('- 31day');
            $day_list = [];
            $day_data = [];
            for($i=0;$i<51;$i++){
              $time = $today+$i*24*60*60;
              $date = date('Y-m-d ',$time).'00:00:00';

             $product_price = DB::table('product_price')
                  ->select('date','top_price','bottom_price','cards')
                  ->where('date',$date)
                  ->where('product_id',$request->product_id)->first();

             if(empty($product_price)) {
                 $day_list[$i]['top_price'] =0;
                 $day_list[$i]['bottom_price'] =0;
                 $day_list[$i]['cards'] =0;
             }else{
                 $day_list[$i]['top_price'] =$product_price->top_price;
                 $day_list[$i]['bottom_price'] =$product_price->bottom_price;
                 $day_list[$i]['cards'] =$product_price->cards;
             }
              $day_list[$i]['time'] = $time;
              $day_list[$i]['date'] = $date;
            }

           return view('/admin/iframe/price',['day_list'=>$day_list,'product_id'=>$request->product_id]);
       }


       public function price_do(Request $request){
           $input = $request->all();
           DB::table('product_price')->where('product_id',(int)$input['product_id'])->delete();
           $date = $input['date'];
           $top_price = $input['top_price'];
           $bottom_price = $input['bottom_price'];
           $cards = $input['cards'];

           $data = [];
           foreach ($date as $key=>$value){
                $data[$key]['date'] = $date[$key];
                $data[$key]['top_price'] = $top_price[$key];
                $data[$key]['bottom_price'] = $bottom_price[$key];
                $data[$key]['cards'] = $cards[$key];
                $data[$key]['product_id'] = (int)$input['product_id'];
           }

           $bool = DB::table('product_price')->insert($data);
            if($bool){
                 echo '<script language="javascript">alert("添加成功");window.location.href="/admin/product/price?product_id='.$input['product_id'].'";</script>';
            }else{
                echo '<script language="javascript">alert("添加失败");window.location.href="/admin/product/price?product_id='.$input['product_id'].'";</script>';
            }



       }




}

